package com.example.usb.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.usb.MyApplication;



/**
 * Create by liangkun on 2018/11/22
 * Describe : 用于存储一些轻量数据
 */

public class SpUtils {
    public static final String SP_USER_INFO = "usb";//服务
    public static SharedPreferences mSp;

    private static SharedPreferences getSp() {
        if (mSp == null) {
            mSp = MyApplication.getContext().getSharedPreferences(SP_USER_INFO, Context.MODE_PRIVATE);
        }
        return mSp;
    }

    /**
     * 获得boolean的数据,没有时为false
     *
     * @param key
     * @return
     */
    public static boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * 获得boolean的数据
     *
     * @param key
     * @param defValue :如果没有返回的值
     * @return
     */
    public static boolean getBoolean(String key, boolean defValue) {
        SharedPreferences sp = getSp();
        return sp.getBoolean(key, defValue);
    }

    /**
     * 设置boolean值
     *
     * @param key
     * @param value
     */
    public static void putBoolean(String key, boolean value) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * 获得String的数据,没有时为null
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        return getString(key, null);
    }

    /**
     * 获得String的数据
     *
     * @param key
     * @param defValue :如果没有返回的值
     * @return
     */
    public static String getString(String key, String defValue) {
        SharedPreferences sp = getSp();
        return sp.getString(key, defValue);
    }

    /**
     * 设置String值
     *
     * @param key
     * @param value
     */
    public static void putString(String key, String value) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.apply();
    }

    /**
     * 获得int的数据,没有时为-1
     *
     * @param key
     * @return
     */
    public static int getInt(String key) {
        return getInt(key, -1);
    }

    /**
     * 获得int的数据
     *
     * @param key
     * @param defValue :如果没有返回的值
     * @return
     */
    public static int getInt(String key, int defValue) {
        SharedPreferences sp = getSp();
        return sp.getInt(key, defValue);
    }

    /**
     * 设置int值
     *
     * @param key
     * @param value
     */
    public static void putInt(String key, int value) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.apply();
    }
    /**
     * 设置int值
     *
     * @param key
     * @param value
     */
    public static void putLong(String key, long value) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.apply();
    }
}
